package com.example.administrator.szb.fragments.fragment_forTab;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.androidkun.xtablayout.XTabLayout;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.AskActivity;
import com.example.administrator.szb.activity.SearchActivity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.bean.Business;
import com.example.administrator.szb.common.SetStatusBar;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;
import com.example.administrator.szb.fragments.fragment_forWD.FragmentAskContent;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.QTLog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by stone on 2017/12/29.
 */

public class FragmentAsk extends MVPBaseFragment {
    private XTabLayout tabLayout;
    private ImageView new_ask,search;
    private View error_net_ll,contents;
    Button home_fragment_button_djcs_error;
    private FragmentManager fm;
    private List<FragmentAskContent> fragmentAskContentList;
    private List<Business> list;
    private boolean isSuccess = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_ask,null);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(View view) {
        SetStatusBar.setBar(view,getContext());
        tabLayout = (XTabLayout)view.findViewById(R.id.tabLayout);
        error_net_ll = view.findViewById(R.id.error_net_ll);
         home_fragment_button_djcs_error = view.findViewById(R.id.home_fragment_button_djcs_error);
        contents = view.findViewById(R.id.contents);
        new_ask = view.findViewById(R.id.new_ask);
        search = view.findViewById(R.id.search);
        fm = getChildFragmentManager();
    }
    public FragmentTransaction getFragmentTransaction(){

        FragmentTransaction transaction = fm.beginTransaction();

        return transaction;
    }
    @Override
    protected void initEvent() {
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });
        new_ask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                startActivity(new Intent(getActivity(), AskActivity.class));
            }
        });
        home_fragment_button_djcs_error.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doReuest();
            }
        });
        error_net_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doReuest();
            }
        });

        tabLayout.setOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                showFragment(tab.getPosition());
            }

            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(XTabLayout.Tab tab) {

            }
        });


    }

    @Override
    protected void initData() {
        fragmentAskContentList = new ArrayList<>();
        list = new ArrayList<>();

        doReuest();
    }
    private void initTabLayout(Business business){
        tabLayout.addTab(tabLayout.newTab().setText(business.getTitle()));
    }

    private void showFragment(int i){
        FragmentTransaction transaction = getFragmentTransaction();
        hideFragment(transaction);
        if (fragmentAskContentList.get(i)==null){
            FragmentAskContent fragmentAskContent = new FragmentAskContent();
            fragmentAskContent.setDatas(list.get(i));
            fragmentAskContentList.set(i,fragmentAskContent);


            transaction.add(R.id.askcontent,fragmentAskContentList.get(i));
        }else {
            transaction.show(fragmentAskContentList.get(i));
        }
        transaction.commitAllowingStateLoss();
    }

    private void hideFragment(FragmentTransaction transaction){
        for (int i=0;i<fragmentAskContentList.size();i++){
            if (fragmentAskContentList.get(i)!=null){
                transaction.hide(fragmentAskContentList.get(i));
            }
        }
    }

    private void doReuest(){
        Map<String,Object> params = new HashMap<>();
        params.put("code", HttpUtil.getParam());
        params.put("business_id", "0");
        HttpUtils.post(activity,URLAddress.URLBASE + URLAddress.URL_Business, params, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                error_net_ll.setVisibility(View.GONE);
                contents.setVisibility(View.VISIBLE);
                list.clear();
                fragmentAskContentList.clear();
                tabLayout.removeAllTabs();
                //全部
                Business business = new Business();
                business.setId(0);
                business.setTitle("全部");
                list.add(business);
                list.addAll(JSON.parseArray(str,Business.class));
                for (int i=0;i<list.size();i++){
                    fragmentAskContentList.add(null);
                    initTabLayout(list.get(i));
                }
                QTLog.e(list.size()+",,,,"+fragmentAskContentList.size());
                //showFragment(0);
            }

            @Override
            public void onFailure(int requestCode, String str) {
                contents.setVisibility(View.GONE);
                error_net_ll.setVisibility(View.VISIBLE);
            }
        });
    }
}
